<?php


namespace app\index\controller;


use app\common\controller\Basecontroller;
use app\index\model\Certificate;
use app\index\model\Collection;
use app\index\model\Company;
use app\index\model\Experience;
use app\index\model\Paf;
use app\index\model\Project;
use app\index\model\Study;
use app\index\model\User;
use app\index\model\Userstatus;
use app\validate\Vali;
use think\Cache;
use think\Request;

class Personal extends Basecontroller
{

    /**
     * 个人页，获取用户所有信息接口
     * ywh开发
     * 接口是否开发完：否
     * @param Userstatus $userState
     * @return \think\response\Json
     */
    public function getUserAttr(Userstatus $userState){
        $phone = $this->getUserPhone();
        return $this->returnJson(self::OK,'请求成功',$userState->getUserState($phone));
    }

    /**
     * 个人页，修改用户基本信息接口
     * ywh开发
     * 接口是否开发完：否
     * @param User $userModel
     * @param Vali $Validate
     * @param Userstatus $userState
     * @return \think\response\Json
     */
    public function editUserAttr(User $userModel , Userstatus $userState , Vali $Validate){
        try {
            $phone = $this->getUserPhone();
            $userData = [
                'username'        =>      input('post.userName'),
                'sex'                  =>      input('post.sex'),
                'identity'            =>      input('post.identity'),
                'birthday'          =>       input('post.birthday'),
                'email'               =>       input('post.email'),
                'phone'             =>       input('post.phone'),
                'workplace'       =>       input('post.workplace')
            ];
            if (!$Validate->scene('editUserAttr')->batch()->check($userData)) return $this->returnJson(self::ERROR,'数据格式错误',$Validate->getError());
            if (!$userModel->humanPush($phone,$userData)) return $this->returnJson(self::ERROR,'数据存储失败');
            return $this->returnJson(self::OK,'修改成功');
        }catch (\Exception $exception){
            throw new $exception;
        }
    }

    /**
     * 修改用户情况接口
     * ywh开发
     * 是否开发完：否
     * @param Request $request
     * @param Userstatus $userState
     * @param Vali $Validate
     */
    public function editUserState(Request $request , Userstatus $userState , Vali $Validate){
        $userID = $this->getUserID();
        $data = [
            'status'                     =>              $request->post('status'),
            'ability'                     =>              $request->post('ability'),
            'advantage'              =>              $request->post('advantage'),
            'exp_jobtype'            =>              $request->post('jobType'),
            'exp_job'                   =>              $request->post('job'),
            'exp_moeny'             =>              $request->post('money'),
            'exp_nature'             =>              $request->post('nature'),
        ];
        $save = array();
        foreach ($data as $key => $value){
            if (!empty($data[$key])){
                $save = [$key => $data[$key]] + $save;
            }
        }
        if (!$Validate->scene('editUserState')->batch()->check($save)) return $this->returnJson(self::ERROR,'数据格式错误',$Validate->getError());
        if (!$userState->editState($userID,$save)) return $this->returnJson(self::ERROR,'服务器错误');
        return $this->returnJson(self::OK,'修改成功');
    }

    /**
     * 修改用户实习经历接口
     * ywh开发
     * 接口开发是否完成：否
     * @param Request $request
     * @param Experience $experience
     * @param Vali $Validate
     * @return \think\response\Json
     *
     */
    public function editTemp(Request $request , Experience $experience , Vali $Validate){
        $human = $this->getUserID();
        $data = [
            'company_name'           =>              $request->post('tempName'),
            'com_class'                   =>               $request->post('tempClass'),
            'department'                  =>               $request->post('dept'),
            'job'                               =>                $request->post('job'),
            'injob_time'                    =>                $request->post('inJob'),
            'outjob_time'                  =>                $request->post('outJob'),
        ];
        if (!$Validate->scene('editTemp')->batch()->check($data)) return $this->returnJson(self::ERROR,'数据格式错误',$Validate->getError());
        if (!$experience->upTemp($human,$data)) return $this->returnJson(self::ERROR,'数据库错误');
        return $this->returnJson(self::OK,'修改成功');
    }

    /**
     * 修改和添加项目经验接口
     * Ywh开发
     * 接口是否完成：否
     * @param Request $request
     * @param Project $project
     * @param Vali $Validate
     * @return \think\response\Json
     */
    public function editProject(Request $request , Project $project , Vali $Validate){
        $human = $this->getUserID();
        $data = [
            'project'            =>          $request->post('project'),
            'pro_name'       =>          $request->post('proName'),
            'mypro'             =>          $request->post('myPro')
        ];
        if (!$Validate->scene('editProject')->batch()->check($data)) return $this->returnJson(self::ERROR,'数据格式错误',$Validate->getError());
        if (!$project->upProject($human,$data)) return $this->returnJson(self::ERROR,'数据库错误');
        return $this->returnJson(self::OK,'修改成功');
    }

    /**
     * 编辑个人页中用户的学习经历
     * @param Request $request
     * @param Study $study
     * @param Vali $Validate
     * @return \think\response\Json
     */
    public function editStudy(Request $request , Study $study , Vali $Validate){
        $human = $this->getUserID();
        $data = [
            'school'                      =>               $request->post('school'),
            'level'                         =>               $request->post('level'),
            'subject'                     =>               $request->post('subject'),
            'school_time'              =>               $request->post('schoolTime'),
            'out_school'               =>               $request->post('outSchool'),
            'in_school'                  =>               $request->post('inSchool'),
        ];
        if (!$Validate->scene('editStudy')->batch()->check($data)) return $this->returnJson(self::ERROR,'数据格式错误',$Validate->getError());
        if (!$study->upStudy($human,$data)) return $this->returnJson(self::ERROR,'数据库错误');
        return $this->returnJson(self::OK,'修改成功');
    }

    /**
     * 修改用户资格证书接口
     * ywh开发
     * 接口是否完成：否
     * @param Request $request
     * @param Certificate $certificate
     * @param Vali $Validate
     * @return \think\response\Json
     */
    public function editSSL(Request $request , Certificate $certificate , Vali $Validate){
        $human = $this->getUserID();
        $data = [
            'certificate'                    =>              $request->post('certificate'),
            'mechanism'                 =>              $request->post('mechanism'),
            'grade'                          =>              $request->post('grade'),
            'is_check'                     =>              $request->post('is_check')
        ];
        if (!$Validate->scene('editSSL')->batch()->check($data)) return $this->returnJson(self::ERROR,'数据格式错误',$Validate->getError());
        if (!$certificate->editCer($human,$data)) return $this->returnJson(self::ERROR,'数据库错误');
        return $this->returnJson(self::OK,'修改成功');
    }

    /**
     * 获取用户收藏职位接口
     * ywh开发
     * @param Collection $collection
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\exception\DbException
     */
    public function getUserColl(Collection $collection , Request $request){
        $human = $this->getUserID();
        $currPage = $request->post('currPage');
        return $this->returnJson(self::OK,'返回用户收藏职位的信息',$collection->getCollAttr($human,$currPage));
    }

    /**
     * 个人页取消收藏接口
     * ywh开发
     *接口是否开发完：否
     *
     * @param Collection $collection
     * @param Request $request
     * @return \think\response\Json
     */
    public function editUserColl(Collection $collection , Request $request){
        try {
            $human = $this->getUserID();
            $data =[
                'offName'  => $request->post('offName'),
                'is_delete' => $request->post('state')
            ];
            if (!$collection->deleteColl($human,$data)) return $this->returnJson(self::ERROR,'数据库错误');
            return $this->returnJson(self::OK,'修改成功');
        }catch (\Exception $exception){
            throw new $exception;
        }
    }

    /**
     * 修改手机号接口
     * ywh开发
     * @param Request $request
     * @param User $userModel
     * @param Cache $cache
     * @return \think\response\Json
     */
    public function modifyPhone(Request $request , User $userModel , Cache $cache){
        $userID = $this->getUserID();
        $data = [
            'phone'         =>          $request->post('phone'),
            'code'         =>            $request->post('code')
        ];
        $redisCode = $cache->get($data['phone']);
        if (!$redisCode) return $this->returnJson(self::ERROR,'请先发送验证码');
        if ($userModel->getOneAttr('phone','=',$data['phone'])) return $this->returnJson(self::ERROR,'手机号已存在');
        if ($redisCode != $data['code']) return $this->returnJson(self::ERROR,'验证码错误');
        if (!$userModel->upPhone($userID,['phone' => $data['phone']])) return $this->returnJson(self::ERROR,'服务器错误');
        return $this->returnJson(self::JUMP,'修改手机号成功');
    }

    /**
     * 安全中心修改密码
     * ywh开发
     * @param Request $request
     * @param Cache $cache
     * @param User $userModel
     * @param Vali $Validate
     * @return \think\response\Json
     */
    public function securityPass(Request $request , Cache $cache , User $userModel , Vali $Validate){
        $data = [
            'phone'         =>          $request->post('phone'),
            'code'           =>          $request->post('code'),
            'password'     =>         $request->post('editPass')
        ];
        $salt = md5(rand(1000,9999));
        if (!$Validate->scene('securityPass')->check($data)) return $this->returnJson(self::ERROR,'数据格式错误',$Validate->getError());
        $redisCode = $cache->get($data['phone']);
        $result = $userModel->getOneAttr('phone','=',$data['phone']);
        if (!$result) return $this->returnJson(self::ERROR,'该账号不存在');
        if ($data['code'] != $redisCode) return $this->returnJson(self::ERROR,'验证码不正确');
        if (!$userModel->humanPush($data['phone'],['password' => md5($salt . md5($data['password'])) , 'salt' => $salt])) return $this->returnJson(self::ERROR,'数据库错误');
        return $this->returnJson(self::OK,'密码修改成功');
    }

    /**
     * 人事申请接口
     * ywh开发
     * @param Request $request
     * @param Vali $Validate
     * @param Company $company
     * @param Userstatus $userState
     * @param Paf $paf
     * @param Cache $cache
     * @return \think\response\Json
     */
    public function judgeImgCode(Request $request , Vali $Validate , Company $company , Userstatus $userState , Paf $paf , Cache $cache){
        $human = $this->getUserID();
        $data = [
            'invCode'         =>            $request->post('invCode'),
            'code'         =>            $request->post('code')
        ];
        $userState_id = $userState->getStateId($human)->toArray()['userstatus_id'];
        $company_id = $company->getInvCode($data['invCode'])->toArray()['company_id'];
        if (!$company_id) return $this->returnJson(self::ERROR,'公司邀请码不存在');
        if ($cache->get('Code') != $data['code']) return $this->returnJson(self::ERROR,'验证码错误');
        $pafMess = ['user_id' => $human , 'userstatus_id' => $userState_id , 'company_id' => $company_id];
        if (!$paf->save($pafMess)) return $this->returnJson(self::ERROR,'数据库错误');
        return $this->returnJson(self::OK,'申请成功');
    }
}